package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.DWaybillVehicleService;
import cn.huiyunche.base.service.mappers.DWaybillVehicleMapper;
import cn.huiyunche.base.service.model.DWaybillVehicle;
import cn.huiyunche.base.service.model.DWaybillVehicleExample;
import cn.huiyunche.base.service.vo.OrderCostTo;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @FileName: cn.huiyunche.service.base.tms.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/11/2 下午3:25
 */
@Service
public class DWaybillVehicleServiceImpl implements DWaybillVehicleService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DWaybillReceiverServiceImpl.class);

    @Autowired
    private DWaybillVehicleMapper dWaybillVehicleMapper;

    @Override
    public Long add(DWaybillVehicle vehicle) {
        LOGGER.info("add param : {}", vehicle);
        if (null == vehicle) {
            LOGGER.error("add param vehicle must not be null");
            throw new IllegalArgumentException("运单车辆信息不能为空");
        }

        dWaybillVehicleMapper.insertSelective(vehicle);

        return vehicle.getId();
    }

    @Override
    public int update(DWaybillVehicle vehicle) {
        LOGGER.info("update params : {}", vehicle);
        if (null == vehicle) {
            LOGGER.error("update params vehicle must not be null");
            throw new IllegalArgumentException("运单车型信息不能为空");
        }
        return dWaybillVehicleMapper.updateByPrimaryKeySelective(vehicle);
    }

    @Override
    public void updateByDWaybillId(Long waybillId, OrderCostTo orderCostTo) {
        LOGGER.info("updateByDWaybillId param : {}, {}", waybillId, orderCostTo);
        if (null == waybillId) {
            LOGGER.error("updateByDWaybillId param waybillId must not be null");
            throw new IllegalArgumentException("运单主键不能为空");
        }
        if (null == orderCostTo) {
            LOGGER.error("updateByDWaybillId param orderCostTo must not be null");
            throw new IllegalArgumentException("费用信息不能为空");
        }

        DWaybillVehicle vehicle = this.getByDWaybillId(waybillId);
        if (null != vehicle) {
            vehicle.setTankVolume(orderCostTo.getTankvolume());//油箱容量
            vehicle.setFuelStandard(orderCostTo.getFuelstandard());//百公里油耗

            this.update(vehicle);
        }
    }

    @Override
    public DWaybillVehicle getByDWaybillId(Long waybillId) {
        LOGGER.info("getByDWaybillId params : {}", waybillId);
        if (null == waybillId) {
            LOGGER.error("getByDWaybillId param waybillId must not be null");
            throw new IllegalArgumentException("运单主键不能为空");
        }

        DWaybillVehicleExample example = new DWaybillVehicleExample();
        example.createCriteria().andWaybillIdEqualTo(waybillId);

        List<DWaybillVehicle> list = dWaybillVehicleMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }

        return null;
    }
}
